import axios from "../../utils/axios"
// pages/activitymodify/activitymodify.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    title:'',
    fail:false,
    fileList:[],
    type:"学习",
    scal:0,
    place:'',
    introduction:'',
    rule:"",
    form:"",
    recommend:"",
    youget:"",
    startdate:"请选择",
    enddate:"请选择",
    hoster:"",
    types:["学习","娱乐","交友"],
    tabbar:['活动信息','活动详情','推荐理由'],
    currentindex:0,
    collect:false,
    activity:[false,false,false,false,false],
    activitykeys:[{key:"introduction",name:"活动介绍"},{key:"rule",name:"活动规则"},{key:"form",name:"活动形式"}],
    activitykeys1:[{key:"recommend",name:"活动评论"},{key:"youget",name:"你将获得"}],
    informationRequestDto:{title:"",welcomeMessage:"",needName:false,needSex:false,needAge:false,needSchool:false,needSps:false,needGrade:false,needPhone:false,needEmail:false,needWechat:false,customerQuestion:[]}
  },
 async getdetail(){
    var that=this
  var activitydetail=await axios.get("/activity/detail?id="+this.data.id)
        var start=new Date(activitydetail.start)
        var end=new Date(activitydetail.end)
        var startstr=start.getFullYear()+"-"+(start.getMonth()+1)+"-"+start.getDate()
        var endstr=end.getFullYear()+"-"+(end.getMonth()+1)+"-"+end.getDate()
        that.setData({
          title:activitydetail.title,
          collect:activitydetail.collectOrNot,
          enddate:endstr,
          startdate:startstr,
          form:activitydetail.form,
          hoster:activitydetail.organizer,
          informationRequestDto:activitydetail.informationRequestDto,
          introduction:activitydetail.introduction,
          rule:activitydetail.rule,
          scal:activitydetail.count,
          type:activitydetail.type,
          place:activitydetail.location
        })
        var activtity=[activitydetail.introduction,activitydetail.rule,activitydetail.form]
        var activtity1=[activitydetail.recommand]
        var fileList=[]
        activitydetail.picture.forEach(item => {
          var file={
            tempFilePath:item
          }
          fileList.push(file)
        });
        this.setData({
          fileList:fileList,
          activitycontent:activtity,
          activitycontent1:activtity1
        })
  },
  getactivitydetail:function(e){
    var id=e.currentTarget.id
    console.log("id",id)
    var type=e.type
    console.log("type",type)
      var value=e.detail.value
      console.log(value+"value")
      this.setData({
        [id]:value
      })
    
    
  },
  changetype:function(e){
    var type=e.currentTarget.dataset.type
    console.log(type,"type")
    this.setData({
      type:type
    })
  },
  changescal:function(e){
    this.setData({
      scal:e.detail
    })
    console.log(e.detail)
  },
  iscollect:function(e){
    this.setData({
      collect:!this.data.collect
    })
  },
  showtext:function(e){
    var index=e.currentTarget.dataset.index
    this.setData({
      ['activity['+index+']']:!this.data.activity[index]
    })
  },
  addquestion:function(){
    var customerQuestion=this.data.informationRequestDto.customerQuestion
    customerQuestion.push("")
    this.setData({
      "informationRequestDto.customerQuestion":customerQuestion
    })
  },
  del(event){
    let id = event.detail.index   //能获取到对应的下标
    let fileList = this.data.fileList  //这里是前端页面展示的数组
    fileList.splice(id,1)    //根据下标来删除对应的图片
    this.setData({
      fileList:fileList,  //在这里进行重新赋值  删除后 图片剩几张就相当于给后台传几张
    })
 },
  getimage(e){
    console.log(e.detail)
    var fileList=this.data.fileList
    console.log(e.detail.file)
    var files=e.detail.file
    files.forEach(item => {
      item.url=item.path
      fileList.push(item)
    });
    this.setData({
      fileList:fileList
    })
  },
  changeindex:function(e){
    var index=e.currentTarget.dataset.index
    this.setData({
      currentindex:index
    })
  },
  changeform:function(e){
    var value=e.currentTarget.dataset.value
    var id="informationRequestDto."+e.currentTarget.id
    this.setData({
      [id]:value
    })
  },
  sumbit:function(){
    var that=this
   wx.showModal({
     title:"是否确认发布",
     success:async function(res){
     if(res.confirm){
      let fileList=that.data.fileList
      var files=[];  //构建的promise数组
       fileList.forEach(item=>{  //item为图片的url
        var file={filePath:item.tempFilePath}
        files.push(file)
       })
       var user=getApp().globalData.user
       console.log(fileList)
       const field={
        title:that.data.title,
        type:that.data.type,
        start:that.data.startdate+" 00:00:00",
        end:that.data.enddate+" 00:00:00",
        count:that.data.scal,
        location:that.data.place,
        organizer:that.data.hoster,
        rule:that.data.rule,
        form:that.data.form,
        collectOrNot:that.data.collect,
        recommand:that.data.recommend,
        reaper:that.data.youget,
        school:user.school,
        introduction:that.data.introduction,
        "informationRequestDto.title":that.data.informationRequestDto.title,
        "informationRequestDto.welcomeMessage":that.data.informationRequestDto.welcomeMessage,
        "informationRequestDto.needName":that.data.informationRequestDto.needName,
        "informationRequestDto.needSex":that.data.informationRequestDto.needSex,
        "informationRequestDto.needAge":that.data.informationRequestDto.needAge,
        "informationRequestDto.needSchool":that.data.informationRequestDto.needSchool,
        "informationRequestDto.needSps":that.data.informationRequestDto.needSps,
        "informationRequestDto.needGrade":that.data.informationRequestDto.needGrade,
        "informationRequestDto.needPhone":that.data.informationRequestDto.needPhone,
        "informationRequestDto.needEmail":that.data.informationRequestDto.needEmail,
        "informationRequestDto.needWechat":that.data.informationRequestDto.needWechat,
        "informationRequestDto.conclusion":"test",
        "informationRequestDto.customerQuestion":that.data.informationRequestDto.customerQuestion
        }
        files.map((item) => {
          return Object.assign(item, {
            name: "files",filePath:item.filePath })
        });
        var fields = Object.keys(field).map(key => ({
          name: key,
          value: field[key]
        }))
        console.log(fields)
       axios.upload('/activity',fields,files)
       wx.navigateBack({
         delta:1
       })
     }
     }
   })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad:function(option){
    var id=JSON.parse(option.id)
    var fail=JSON.parse(option.fail)
    this.setData({
      id:id,
      fail:fail
    })
    this.getdetail()
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})